﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TradingServer.Core
{
    public static class Extensions
    {
		public static void SafeInvoke(this Action source)
		{
			var action = source;
			if (action != null) action();
		}

        public static IAsyncResult SafeInvokeAsync(this Action source)
        {
            var action = source;
			if (action != null) return action.BeginInvoke(null, action);
            return null;
        }
        
        public static void SafeInvoke<T>(this Action<T> source, T param)
        {
            var action = source;
            if (action != null) action(param);
        }

		public static void SafeInvoke<T1, T2>(this Action<T1, T2> source, T1 param1, T2 param2)
		{
			var action = source;
			if (action != null) action(param1, param2);
		}

        public static IAsyncResult SafeInvokeAsync<T>(this Action<T> source, T param)
        {
            var action = source;
			if (action != null) return action.BeginInvoke(param, null, action);
            return null;
        }

		public static IAsyncResult SafeInvokeAsync<T1, T2>(this Action<T1, T2> source, T1 param1, T2 param2)
		{
			var action = source;
			if (action != null) return action.BeginInvoke(param1, param2, null, action);
			return null;
		}
    }
}
